package com.zhoug.imageloader3;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;

import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.graphics.drawable.RoundedBitmapDrawable;
import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory;
import jp.wasabeef.glide.transformations.GrayscaleTransformation;


/**
 * 图片加载器
 * @Author HK-LJJ
 * @Date 2019/12/11
 * @Description
 */
public class ZImageLoader {
    private static final String TAG = ">>>IImageLoader";
    private static  boolean debug=false;

    public static void setDebug(boolean debug) {
        ZImageLoader.debug = debug;
    }

    /**
     * 加载图片
     * @param context
     * @param imageView
     * @param imageUrl
     */
    public static void load(Context context, ImageView imageView, Object imageUrl){
        load(context, imageView, imageUrl,null ,null ,null );
    }


    /**
     * 加载圆形图片
     *
     * @param context
     * @param imageView
     * @param bitmap
     */
    public static void loadRoundedBitmapDrawable(Context context, ImageView imageView, @NonNull Bitmap bitmap) {
        RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(context.getResources(), bitmap);
        roundedBitmapDrawable.setAntiAlias(true);//抗锯齿
        roundedBitmapDrawable.setCircular(true);//设置为圆形
        imageView.setImageDrawable(roundedBitmapDrawable);
    }

    /**加载图片
     * @param context       上下文环境
     * @param imageView     图片组件
     * @param imageUrl      图片完整地址
     * @param placeholderId 加载占位图
     * @param errorId       加载错误图
     * @param progressBar   显示正在加载框
     */
    public static void load(Context context, ImageView imageView, Object imageUrl, @DrawableRes Integer placeholderId, @DrawableRes Integer errorId, ProgressBar progressBar) {
        if (imageUrl == null && errorId != null) {
            imageView.setImageResource(errorId);
        } else if (imageUrl != null) {
            RequestOptions requestOptions = new RequestOptions();
            if (placeholderId != null) {
                requestOptions = requestOptions.placeholder(placeholderId);
            }
            if (errorId != null) {
                requestOptions = requestOptions.error(errorId);
            }

            RequestBuilder<Drawable> requestBuilder = Glide.with(context).load(imageUrl).apply(requestOptions);
            if (progressBar != null) {
                progressBar.setVisibility(View.VISIBLE);
                requestBuilder = requestBuilder.listener(new RequestListener<Drawable>() {


                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                        if (progressBar != null) {
                            progressBar.setVisibility(View.GONE);
                        }
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                        if (progressBar != null) {
                            progressBar.setVisibility(View.GONE);
                        }
                        return false;
                    }
                });
            }
            requestBuilder.into(imageView);
        }
    }


    /**
     * 加载圆形图片
     * @param context
     * @param imageView
     * @param imageUrl  图片完整地址
     * @param placeholder  图片加载成功之前显示的占位图
     * @param nullImageRes  沒有图片时显示的默认图片
     * @param errorImageRes 加载失败显示的错误图片
     */
    public static void loadCircle(Context context, ImageView imageView, Object imageUrl,@DrawableRes int placeholder, @DrawableRes int nullImageRes,@DrawableRes int errorImageRes) {
        RequestOptions options = RequestOptions.bitmapTransform(new CircleCrop());
        if (imageUrl != null) {
            options=options.placeholder(placeholder).error(errorImageRes);
            Glide.with(context).load(imageUrl).apply(options).into(new CircleImageTarget<>(imageView));
        } else {
            Glide.with(context).load(nullImageRes).apply(options).into(new CircleImageTarget<>(imageView));
        }
    }




    /**
     * 加载圆形图片
     * @param context
     * @param imageView
     * @param imageUrl  图片完整地址
     * @param defaultImageRes 加载失败和沒有图片时显示的默认图片
     */
    public static void loadCircle(Context context, ImageView imageView, Object imageUrl, @DrawableRes int defaultImageRes) {
        loadCircle(context,imageView,imageUrl,defaultImageRes,0,0);
    }


    /**
     *
     * @param context
     * @param imageView
     * @param imageUrl 图片地址
     * @param placeholder 加载之前的占位图
     * @param nullImageRes 没有图片时显示的默认图
     * @param errorImageRes 图片加载失败后显示的错误图
     * @param isGrayscale 是否添加灰色滤镜
     * @param isCircle 是否是圆形图
     */
    public static void load(Context context, ImageView imageView, Object imageUrl,@DrawableRes int placeholder, @DrawableRes int nullImageRes,@DrawableRes int errorImageRes,boolean isGrayscale,boolean isCircle) {
        RequestOptions options= new RequestOptions();
        if(isGrayscale && isCircle){
            options= options.transform(new CircleCrop(),new GrayscaleTransformation());
        }else if(isGrayscale){
            options= options.transform(new GrayscaleTransformation());
        }else if(isCircle){
            options = options.transform(new CircleCrop());
        }
        if (imageUrl != null) {
            options=options.placeholder(placeholder).error(errorImageRes);
            if(isCircle){
                Glide.with(context).load(imageUrl).apply(options).into(new CircleImageTarget(imageView));
            }else{
                Glide.with(context).load(imageUrl).apply(options).into(imageView);
            }
        } else {
            if(isCircle){
                Glide.with(context).load(nullImageRes).apply(options).into(new CircleImageTarget(imageView));
            }else{
                Glide.with(context).load(nullImageRes).apply(options).into(imageView);
            }
        }

           /* RequestOptions options = new RequestOptions();
            if(isGrayscale){
                options = options.transform(new CircleCrop(),new GrayscaleTransformation());
            }else{
                options = options.transform(new CircleCrop());
            }*/
    }




}
